﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace Chip8Dev.CPU
{
    public sealed partial class Interpreter : ICpuEngine
    {
        private void Opcode_JP_1(Instruction inst)
        {
            DebugWrite(String.Format("Jump to {0:4X}", inst.NNN));
            m_State.PC = inst.NNN;
        }

        private void Opcode_Call(Instruction inst)
        {
            DebugWrite(String.Format("Routine call at {0:4X}", inst.NNN));
            m_RoutineStack.Push(m_State.PC + 2);
            m_State.PC = inst.NNN;
        }

        private void Opcode_SE_5(Instruction inst)
        {
            DebugWrite(String.Format("Skip if V{0:2X} = V{1:2X}", inst.X, inst.Y));

            if (m_State.VRegisters[inst.X] == m_State.VRegisters[inst.Y])
            {
                m_State.PC += 2;
            }
        }

        private void Opcode_SE_3(Instruction inst)
        {
            DebugWrite(String.Format("Skip if V{0:2X} = {1:2X}", inst.X, inst.KK));

            if (m_State.VRegisters[inst.X] == inst.KK)
            {
                m_State.PC += 2;
            }
        }

        private void Opcode_SNE_4(Instruction inst)
        {
            DebugWrite(String.Format("Skip if V{0:2X} != {1:2X}", inst.X, inst.KK));

            if (m_State.VRegisters[inst.X] != inst.X)
            {
                m_State.PC += 2;
            }
        }

        private void Opcode_SNE_9(Instruction inst)
        {
            DebugWrite(String.Format("Skip if V{0:2X} != V{1:2X}", inst.X, inst.Y));

            if (m_State.VRegisters[inst.X] != m_State.VRegisters[inst.Y])
            {
                m_State.PC += 2;
            }
        }

        private void Opcode_JP_B(Instruction inst)
        {
            DebugWrite(String.Format("Jump to {0:4X}", inst.NNN + m_State.VRegisters[0]));

            m_State.PC = inst.NNN + m_State.VRegisters[0];
        }

        private void Opcode_SKP(Instruction inst)
        {
            DebugWrite(String.Format("Skip if key press is equal to V{0:2X}", inst.X));

            if (m_State.VRegisters[inst.X] == m_State.KeyPress)
            {
                m_State.PC += 2;
            }
        }

        private void Opcode_SKNP(Instruction inst)
        {
            DebugWrite(String.Format("Skip if key press is not equal to V{1:2X}", inst.X));

            if (m_State.VRegisters[inst.X] != m_State.KeyPress)
            {
                m_State.PC += 2;
            }
        }
    }
}
